#include "weatherdata.h"
#include <QDebug>

// 实现CityInfo类的解析函数
CityInfo CityInfo::fromJson(const QJsonObject &json) {
    CityInfo info;
    info.city = json["city"].toString();
    info.cityKey = json["citykey"].toString();
    info.parent = json["parent"].toString();
    info.updateTime = json["updateTime"].toString();
    return info;
}

// 实现Forecast类的解析函数
Forecast Forecast::fromJson(const QJsonObject &json) {
    Forecast forecast;
    forecast.date = json["date"].toString();
    forecast.high = json["high"].toString();
    forecast.low = json["low"].toString();
    forecast.ymd = QDate::fromString(json["ymd"].toString(), "yyyy-MM-dd");
    forecast.week = json["week"].toString();
    forecast.sunrise = QTime::fromString(json["sunrise"].toString(), "hh:mm");
    forecast.sunset = QTime::fromString(json["sunset"].toString(), "hh:mm");
    forecast.aqi = json["aqi"].toInt();
    forecast.fx = json["fx"].toString();
    forecast.fl = json["fl"].toString();
    forecast.type = json["type"].toString();
    forecast.notice = json["notice"].toString();
    return forecast;
}

// 实现Yesterday类的解析函数
Yesterday Yesterday::fromJson(const QJsonObject &json) {
    Yesterday yesterday;
    yesterday.date = json["date"].toString();
    yesterday.high = json["high"].toString();
    yesterday.low = json["low"].toString();
    yesterday.ymd = QDate::fromString(json["ymd"].toString(), "yyyy-MM-dd");
    yesterday.week = json["week"].toString();
    yesterday.sunrise = QTime::fromString(json["sunrise"].toString(), "hh:mm");
    yesterday.sunset = QTime::fromString(json["sunset"].toString(), "hh:mm");
    yesterday.aqi = json["aqi"].toInt();
    yesterday.fx = json["fx"].toString();
    yesterday.fl = json["fl"].toString();
    yesterday.type = json["type"].toString();
    yesterday.notice = json["notice"].toString();
    return yesterday;
}

// 实现WeatherData类的解析函数
WeatherData WeatherData::fromJson(const QJsonObject &json) {
    WeatherData data;
    data.message = json["message"].toString();
    data.status = json["status"].toInt();
    data.date = QDate::fromString(json["date"].toString(), "yyyyMMdd");
    data.time = QDateTime::fromString(json["time"].toString(), "yyyy-MM-dd hh:mm:ss");
    data.cityInfo = CityInfo::fromJson(json["cityInfo"].toObject());

    QJsonObject dataObj = json["data"].toObject();
    data.shidu = dataObj["shidu"].toString();
    data.pm25 = dataObj["pm25"].toDouble();
    data.pm10 = dataObj["pm10"].toDouble();
    data.quality = dataObj["quality"].toString();
    data.wendu = dataObj["wendu"].toDouble();
    data.ganmao = dataObj["ganmao"].toString();

    // 解析forecast数组
    QJsonArray forecastArray = dataObj["forecast"].toArray();
    data.forecast.reserve(forecastArray.size());
    for (const auto &item : forecastArray) {
        data.forecast.append(Forecast::fromJson(item.toObject()));
    }

    // 解析yesterday
    data.yesterday = Yesterday::fromJson(dataObj["yesterday"].toObject());

    return data;
}    